$(document).ready(function(){
//	var ifQuestionnaireSaved=$("#ifQuestionnaireSaved").val();
//	alert(ifQuestionnaireSaved);
	var chosedUser=$("#chosedUser").val();
//给问题载入选项
$(".question").each(function(){
	var questionId=$(this).children(".questionId").val();
	var questionType=$(this).children(".questionType").val();
//	alert("questionType:"+questionType);
//	alert("questionId:"+questionId);
//	console.info("questionId:"+questionId);
	var multiChoiceOpton="";
	var singleChoiceOpton="";
	
	//questionId和userId去answer表找答案answerText，返回string，填入
	//获取本体答案
	var answerText="";
	if(questionType==0){
//		alert("11111");
		$this=$(this);
		var textarea="<textarea class='textarea' cols='80' rows='10'></textarea><input class='questionId' type='hidden' value="+questionId+">";
	$.ajax({
		url : "getAnswerTextByUserIdAndQuestionId2.action?questionId=" + questionId+"&chosedUser="+chosedUser,
		 async:false,
		 cache:false,
//		 dataType:'json',
//		 scriptCharset: 'utf-8',
//		 contentType:'application/x-www-form-urlencoded; charset=UTF-8',
		success:function(result){
			var answer=JSON.parse(result);
//			alert("00000answerText:"+answerText);
			if(answer!=null){
				textarea="<textarea class='textarea' cols='80' rows='10'>"+answer.answerText+"</textarea><input class='questionId' type='hidden' value="+questionId+">";
			}
			$this.children(".option").empty();
			$this.children(".option").append(textarea);
//			alert("9999999999");
		}
    });
//	$this.children(".option").append(textarea);
	}
//	alert("answerText:"+answerText);
//	if(answerText!="" && answerText!=null){
//		alert("yes");
//		var textarea="<textarea class='textarea' cols='80' rows='10'>"+answerText+"</textarea><input class='questionId' type='hidden' value="+questionId+">";
//	}
//	else{
//		alert("no");
//		var textarea="<textarea class='textarea' cols='80' rows='10'></textarea><input class='questionId' type='hidden' value="+questionId+">";
//	}
//	var textarea="<textarea class='textarea' cols='80' rows='10'>"+answerText+"</textarea><input class='questionId' type='hidden' value="+questionId+">";    
	if(questionType==1 || questionType==2){
	$this=$(this);
	$.ajax({
		url:"getOptionByQuestion.action?questionId="+questionId,
		cache:false,
		async :false,
		success: function(result){
			var dataObject=JSON.parse(result);
//			var option="";\
			radioName="question"+questionId;
			$.each(dataObject,function(key,value) {
				
				//ajax根据optionId和userId从answer表找得到 则有记录，否则没有答案，返回answer类型
				$.ajax({
					url : "getAnswerByUserIdAndOptionId2.action?optionId=" + value.optionId+"&chosedUser="+chosedUser,
					 async:false,
					success:function(result){
						var answerObject=JSON.parse(result);
//						alert("answerObject:"+answerObject);
						if(answerObject!=null){
//						if(answerObject!="" && answerObject!=null){
							//选中的多选选项
//							multiChoiceOpton +="<div class='option'><input type='checkbox' value="+value.optionId+">"+value.optionNumber+":"+value.optionName+"</div>"
							multiChoiceOpton+="<div class='option'><input type='checkbox' checked value="+value.optionId+"><input class='questionId' type='hidden' value="+questionId+">"+value.optionNumber+":"+value.optionName+"</div>";
							//选中的单选选项
							singleChoiceOpton +="<div class='option'><input type='radio' checked name="+radioName+" value="+value.optionId+"><input class='questionId' type='hidden' value="+questionId+">"+value.optionNumber+":"+value.optionName+"</div>";
						}
						else{
							//没选中的多选选项
//							multiChoiceOpton +="<div class='option'><input type='checkbox' value="+value.optionId+">"+value.optionNumber+":"+value.optionName+"</div>"
							multiChoiceOpton+="<div class='option'><input type='checkbox' value="+value.optionId+"><input class='questionId' type='hidden' value="+questionId+">"+value.optionNumber+":"+value.optionName+"</div>";
							//没选中的单选选项
							singleChoiceOpton +="<div class='option'><input type='radio' name="+radioName+" value="+value.optionId+"><input class='questionId' type='hidden' value="+questionId+">"+value.optionNumber+":"+value.optionName+"</div>";
						}
					}
			    });
				
				//多项选择
//				multiChoiceOpton +="<div class='option'><input type='checkbox' value="+value.optionId+">"+value.optionNumber+":"+value.optionName+"</div>"
//				multiChoiceOpton+="<div class='option'><input type='checkbox' value="+value.optionId+"><input class='questionId' type='hidden' value="+questionId+">"+value.optionNumber+":"+value.optionName+"</div>";
				//单项选择
//				singleChoiceOpton +="<div class='option'><input type='radio' name="+radioName+" value="+value.optionId+"><input class='questionId' type='hidden' value="+questionId+">"+value.optionNumber+":"+value.optionName+"</div>";
			}); 
			
			$this.children(".option").empty();
//			alert(questionType);
			//根据选择题类型载入选项
			switch (questionType)
			{
			case '2':
				$this.children(".option").append(multiChoiceOpton);
			  break;
			case '1':
				$this.children(".option").append(singleChoiceOpton);
			  break;
			default:
				$this.children(".option").append("QuestionType is not right,Please check your questionType in backend management console by website administrator!!");
			}
			}
	});
	}
	});
});